<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--查询-->
      <el-form
        :model="queryParams"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="租户名" prop="distrName">
          <el-input
            v-model="queryParams.distrName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="queryParams.userName"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="租户类型" prop="distrType">
          <!-- 租户类型 sys_user:平台 seller:卖家 distributor:商家 -->
          <el-select @change="handleQuery" v-model="queryParams.distrType">
            <el-option label="平台" :value="'sys_user'" />
            <el-option label="卖家" :value="'seller'" />
            <el-option label="商家" :value="'distributor'" />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <!-- 帐号状态 0:正常 1:停用 -->
          <el-select @change="handleQuery" v-model="queryParams.status">
            <el-option label="正常" :value="0" />
            <el-option label="停用" :value="1" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </el-row>
    <el-row :gutter="10" class="mb8">
      <!-- 所有用户管理---新增平台用户废弃 -->
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['distribution:user:insertUserByPlatform']"
        >
          新增平台用户
        </el-button>
      </el-col> -->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table
      :height="calTableHeight()"
      v-loading="loading"
        ref="TableRef"  :data="tableData"
      @selection-change="(rows) => (tableCheck = rows)"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="50"
        align="center"
      />
      <!-- 租户类型 租户id 租户名称 用户名 用户角色 状态 最后登录时间 最后登录IP 操作 -->
      <!-- 租户类型 -->
      <el-table-column
        show-overflow-tooltip
        label="租户类型"
        align="center"
        prop="distrType"
      >
        <template slot-scope="{ row }">
          <!-- 租户类型 sys_user:平台 seller:卖家 distributor:商家 -->
          <span style="color: #409eff" v-if="row.distrType == 'sys_user'">
            平台
          </span>
          <span style="color: #67c23a" v-if="row.distrType == 'seller'">
            卖家
          </span>
          <span style="color: #e6a23c" v-if="row.distrType == 'distributor'">
            商家
          </span>
        </template>
      </el-table-column>
      <!-- 租户id -->
      <el-table-column
        show-overflow-tooltip
        label="租户id"
        align="center"
        prop="distrId"
      />
      <!-- 租户名称 -->
      <el-table-column
        show-overflow-tooltip
        label="租户名称"
        align="center"
        prop="distrName"
      />
      <!-- 用户名 -->
      <el-table-column
        show-overflow-tooltip
        label="用户名"
        align="center"
        prop="userName"
      />
      <!-- email -->
      <el-table-column
        show-overflow-tooltip
        label="email"
        align="center"
        prop="email"
      />
      <!-- 用户角色 -->
      <el-table-column
        show-overflow-tooltip
        label="用户角色"
        align="center"
        prop="roleName"
      />
      <!-- 状态 -->
      <el-table-column
        show-overflow-tooltip
        label="状态"
        align="center"
        prop="status"
      >
        <template slot-scope="{ row }">
          <!-- 帐号状态 0:正常 1:停用 -->
          <el-switch
            v-hasPermi="['distribution:user:updateStatus']"
            v-model="row.status"
            :active-value="'0'"
            :inactive-value="'1'"
            @change="handleStatusChange(row)"
          />
        </template>
      </el-table-column>
      <!-- 最后登录时间 -->
      <el-table-column
        show-overflow-tooltip
        label="最后登录时间"
        align="center"
        prop="loginDate"
      />
      <!-- 最后登录IP -->
      <el-table-column
        show-overflow-tooltip
        label="最后登录IP"
        align="center"
        prop="loginIp"
      />
      <el-table-column
        show-overflow-tooltip
        :label="$t('操作')"
        align="center"
        width="160"
      >
        <template slot-scope="scope">
          <!-- 修改信息 修改密码 禁用/启用 修改租户 -->
          <el-dropdown
            @command="(command) => handleCommand(command, scope.row)"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">
              更多
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <!-- v-hasPermi="['system:user:resetPwd']" -->
              <!-- <el-dropdown-item
                v-hasPermi="['distribution:user:updateUserByPlatform']"
                command="handleUpdate"
                icon="el-icon-key"
              >
                修改信息
              </el-dropdown-item> -->
              <el-dropdown-item
                v-hasPermi="['distribution:user:updateUserByPlatform']"
                command="handleUpdatePwd"
                icon="el-icon-key"
              >
                修改密码
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 新增修改 -->
    <AddAndEdit
      :show.sync="addAndEdit.show"
      @getList="getList"
      :formData="addAndEdit.formData"
      :type="addAndEdit.type"
    />
    <!-- 修改信息 -->
    <EditInfo
      :show.sync="editInfo.show"
      @getList="getList"
      :formData="editInfo.formData"
      :type="editInfo.type"
    />
    <!-- 修改密码 -->
    <EditPassword
      :show.sync="editPassword.show"
      @getList="getList"
      :formData="editPassword.formData"
    />
  </div>
</template>

<script>
import {
  getUserList,
  updateUserByPlatform,
  updateStatus,
} from "@/api/platform/userManage";
import { checkOps } from "@/api/platform/supplierManage";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import AddAndEdit from "./components/addAndEdit.vue";
import EditInfo from "./components/editInfo.vue";
import EditPassword from "./components/editPassword.vue";
export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: "UserManage",
  components: { Treeselect, AddAndEdit, EditInfo, EditPassword },
  data() {
    return {
      /**新增/修改 */
      addAndEdit: {
        show: false,
        type: "",
        formData: {},
      },
      /**修改信息 */
      editInfo: {
        show: false,
        type: "",
        formData: {},
      },
      /**修改密码 */
      editPassword: {
        show: false,
        formData: {},
      },

      tableData: [],
      tableCheck: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        distrName: "",
        userName: "",
        distrType: "",
        status: "",
      },
    };
  },
  watch: {},
  created() {
    this.getList();
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      Object.keys(this.queryParams).forEach((key) => {
        this.queryParams[key] = "";
      });
      this.queryParams.pageSize = 10;
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    // 帐号状态 status  0:正常 1:停用
    handleStatusChange(row) {
      const data = {
        userId: row.userId,
        status: row.status,
      };
      console.log(`data ==>`,JSON.parse(JSON.stringify(data)));
      updateStatus(data)
        .then((res) => {
          const { code, data, msg } = res;
          if (code === 200) {
            this.$message({ type: 'success', message: msg})
;
            this.getList();
          }
        })
        .catch(() => {
          row.status == "0" ? (row.status = "1") : (row.status = "0");
        });
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      getUserList(this.queryParams).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleUpdate" /**修改信息 */:
          this.handleUpdate(row);
          break;
        case "handleUpdatePwd" /**修改密码 */:
          this.editPassword.formData = row;
          this.editPassword.show = true;
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      checkOps(1).then((res) => {
        const { code } = res;
        if (code === 200) {
          this.addAndEdit.type = "add";
          this.addAndEdit.formData = {};
          this.addAndEdit.show = true;
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.addAndEdit.type = "edit";
      this.addAndEdit.formData = row;
      this.addAndEdit.show = true;
    },
  },
};
</script>
